package com.HQCourse.wechat.wechat.controller;


import com.HQCourse.Exception.HQCourseException;
import com.HQCourse.result.Result;
import com.HQCourse.wechat.wechat.service.MenuService;
import com.HQCourse.wechat.wechat.utils.ConstantPropertiesUtil;
import com.HQCourse.wechat.wechat.utils.HttpClientUtils;
import com.alibaba.fastjson.JSONObject;
import com.example.HQCourse.model.wechat.Menu;
import com.example.HQCourse.vo.wechat.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单明细 订单明细 前端控制器
 * </p>
 *
 * @author zww
 * @since 2023-07-04
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/admin/wechat/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @GetMapping("syncMenu")
    public Result createMenu(){
        menuService.syncMenu();
        return Result.ok(null);
    }
    @DeleteMapping("removeMenu")
    public Result removeMenu(){
        menuService.removeMenu();
        return Result.ok(null);
    }


    @GetMapping("getAccessToken")
    public Result getAccessToken(){
        StringBuffer buffer=new StringBuffer();
        //拼接请求地址
        buffer.append("https://api.weixin.qq.com/cgi-bin/token");
        buffer.append("?grant_type=client_credential");
        buffer.append("&appid=%s");
        buffer.append("&secret=%s");
        //请求地址设置参数
        String url = String.format(buffer.toString(),
                ConstantPropertiesUtil.ACCESS_KEY_ID,
                ConstantPropertiesUtil.ACCESS_KEY_SECRET);
        //发送http请求
        String tokenString = null;
        try {
            tokenString = HttpClientUtils.get(url);
        } catch (Exception e) {
            throw new HQCourseException(20001,"发送请求失败");
        }
        //获取access_token
        JSONObject jsonObject = JSONObject.parseObject(tokenString);
        String access_token = jsonObject.getString("access_token");
        //返回
        return Result.ok(access_token);
    }
    //获取所有一级菜单
    @GetMapping("findOneMenuInfo")
    public Result findOneMenu(){
        List<Menu> list=menuService.findOneMenuInfo();
        return Result.ok(list);
    }
    @GetMapping("findMenuInfo")
    public Result findOneMenuInfo(){
        List<MenuVo> lists=menuService.findMenuInfo();
        return Result.ok(lists);
    }
    @GetMapping("get{id}")
    public Result get(@PathVariable Long id){
        return Result.ok(menuService.getById(id));
    }
    @PostMapping("save")
    public Result save(@PathVariable Menu menu){
        menuService.save(menu);
        return Result.ok(null);
    }
    @PutMapping("update")
    public Result update(@PathVariable Menu menu){
        menuService.updateById(menu);
        return Result.ok(null);
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        menuService.removeById(id);
        return Result.ok(null);
    }
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@PathVariable List<Long> idList){
        menuService.removeByIds(idList);
        return Result.ok(idList);
    }
}

